package com.yjc.hire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yjc.bo.SearchBO;
import com.yjc.hire.VO.UsersVO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.bo.ModifyUserBO;
import com.yjc.hire.enums.UserRole;
import com.yjc.hire.exceptions.GraceException;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.mapper.UserMapper;
import com.yjc.hire.service.UserService;
import com.yjc.pojo.Users;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/21
 * </p>
 */

@Service
public class UserServiceImpl extends BaseInfoProperties implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void modifyUserInfo(ModifyUserBO modifyUserBO) {
        String userId = modifyUserBO.getUserId();
        if (StringUtils.isBlank(userId)){
            GraceException.disPlay(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }
        Users pendingUser = new Users();
        pendingUser.setId(userId);
        pendingUser.setUpdatedTime(LocalDateTime.now());
        BeanUtils.copyProperties(modifyUserBO, pendingUser);
        userMapper.updateById(pendingUser);
    }

    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     */
    @Override
    public Users selectUserInfoById(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public Long getHrCountByCompanyId(String companyId) {
        return userMapper.getHrCountByCompanyId(companyId);
    }

    /**
     * 绑定hr与公司的关系
     * @param hrUserId
     * @param companyId
     * @param realName
     */
    @Override
    @Transactional
    public void updateUserCompanyId(String hrUserId, String companyId, String realName) {
        Users hrUser = new Users();
        hrUser.setRealName(realName);
        hrUser.setId(hrUserId);
        hrUser.setHrInWhichCompanyId(companyId);
        hrUser.setUpdatedTime(LocalDateTime.now());
        userMapper.updateById(hrUser);
    }

    @Override
    public Users getByUserId(String hrUserId) {
        return userMapper.selectById(hrUserId);
    }

    @Override
    public void updateUserToHR(String userId) {
        Users users = new Users();
        users.setId(userId);
        users.setUpdatedTime(LocalDateTime.now());
        users.setRole(UserRole.RECRUITER.type);
        userMapper.updateById(users);
    }

    @Override
    public PageGridResult<List<Users>> getHrListByCompanyId(String companyId, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Users> hrList = userMapper.selectHrListByCompanyId(companyId);
        return PageGridResult.of(hrList,page);

    }

    @Override
    @Transactional
    public void updateUserToCand(String userId) {
        Users hrUser = new Users();
        hrUser.setId(userId);
        hrUser.setUpdatedTime(LocalDateTime.now());
        hrUser.setRole(UserRole.CANDIDATE.type);
        hrUser.setHrInWhichCompanyId("0");
        userMapper.updateById(hrUser);
    }

    @Override
    public List<Users> getList(SearchBO searchBO) {
        return userMapper.selectList(
                new LambdaQueryWrapper<Users>()
                        .in(Users::getId, searchBO.getUserIds())
        );
    }
}
